<html><head><title></title></head>
<body bgcolor=#ffffff>
 
<a href="CodeConvTOC.doc.html">[Contents]</a> <a href="CodeConventions.doc3.html">[Prev]</a> <a href="CodeConventions.doc5.html">[Next]</a>
<hr><br>
 
<a name="a385"> </a>
<h2 align="center">5 - 	 Comments</h2>
<p><a name="337"> </a>
Java programs can have two kinds of comments: implementation comments and documentation comments.  Implementation comments are those found in C++, which are delimited by /*...*/, and //.  Documentation comments (known as "doc comments") are Java-only, and are delimited by  /**...*/.  Doc comments can be extracted to HTML files using the javadoc tool.
</p><p><a name="15475"> </a>
Implementation comments are mean for commenting out code or for comments about the particular implementation.  Doc comments are meant to describe the specification of the code, from an implementation-free perspective. to be read by developers who might not necessarily have the source code at hand. 
</p><p><a name="15472"> </a>
Comments should be used to give overviews of code and provide additional information that is not readily available in the code itself. Comments should contain only information that is relevant to reading and understanding the program. For example, information about how the corresponding package is built or in what directory it resides should not be included as a comment.
</p><p><a name="338"> </a>
Discussion of nontrivial or nonobvious design decisions is appropriate, but avoid duplicating information that is present in (and clear from) the code. It is too easy for redundant comments to get out of date. In general, avoid any comments that are likely to get out of date as the code evolves.
</p><a name="599"> </a>
<p>
<b>Note:</b>The frequency of comments sometimes reflects poor quality of code. When you feel compelled to add a comment, consider rewriting the code to make it clearer.
<p><a name="339"> </a>
Comments should not be enclosed in large boxes drawn with asterisks or other characters. <br>Comments should never include special characters such as form-feed and backspace.
</p><a name="a216"> </a>
<h3> 5.1	 Implementation Comment Formats</h3>
<p><a name="340"> </a>
Programs can have four styles of implementation comments: block, single-line, trailing, and end-of-line.
</p><a name="a680"> </a>
<h4>5.1.1	 Block Comments </h4>
<p><a name="343"> </a>
Block comments are used to provide descriptions of files, methods, data structures and algorithms. Block comments may be used at the beginning of each file and before each method. They can also be used in other places, such as within methods. Block comments inside a function or method should be indented to the same level as the code they describe.
</p><p><a name="346"> </a>
A block comment should be preceded by a blank line to set it apart from the rest of the code. 
</p><blockquote><pre>/*
 * Here is a block comment.
 */
<p></pre></blockquote><a name="350"> </a>
Block comments can start with <code>/*-</code>, which is recognized by <strong>indent</strong>(1) as the beginning of a block comment that should not be reformatted. Example:
</p><pre><blockquote>/*-
 * Here is a block comment with some very special
 * formatting that I want indent(1) to ignore.
 *
 *    one
 *        two
 *            three
 */
<a name="556"> </a>
</pre></blockquote><b>Note:</b> If you don't use <strong>indent</strong>(1), you don't have to use <code>/*-</code> in your code or make any other concessions to the possibility that someone else might run <strong>indent</strong>(1) on your code.</blockquote>
<p><a name="279"> </a>
See also <a href="CodeConventions.doc4.html#a16838">"Documentation Comments" on page 9</a>.
</p></pre></blockquote><a name="a341"> </a>
<h4>5.1.2	 Single-Line Comments</h4>
<p><a name="354"> </a>
Short comments can appear on a single-line indented to the level of the code that follows. If a comment can't be written in a single-line, it should follow the block comment format<a href="CodeConventions.doc4.html#a680"> (see section 5.1.1)</a>. A single-line comment should be preceded by a blank line. Here's an example of a single-line comment in Java code (also see <a href="CodeConventions.doc4.html#a16838">"Documentation Comments" on page 9</a>):
</p><blockquote><pre>if (condition) {

    /* Handle the condition. */
    ...
}
</pre></blockquote><a name="a342"> </a>
<h4>5.1.3	 Trailing Comments</h4>
<p><a name="359"> </a>
Very short comments can appear on the same line as the code they describe, but should be shifted far enough to separate them from the statements. If more than one short comment appears in a chunk of code, they should all be indented to the same tab setting.
</p><p><a name="357"> </a>
Here's an example of a trailing comment in Java code: 
</p><blockquote><pre>if (a == 2) {
    return TRUE;            /* special case */
} else {
    return isPrime(a);      /* works only for odd a */
}
</pre></blockquote><a name="a286"> </a>
<h4>5.1.4	 End-Of-Line Comments</h4>
<p><a name="629"> </a>
The <code>//</code> comment delimiter can comment out a complete line or only a partial line.  It shouldn't be used on consecutive multiple lines for text comments; however, it can be used in consecutive multiple lines for commenting out sections of code.  Examples of all three styles follow: 
</p><blockquote><pre>if (foo &gt; 1) {

    // Do a double-flip.
    ...
}
else {
    return false;          // Explain why here.
}
//if (bar &gt; 1) {
//
//    // Do a triple-flip.
//    ...
//}
//else {
//    return false;
//}
</pre></blockquote><a name="a16838"> </a>
<h3> 5.2	 Documentation Comments </h3>
<a name="16842"> </a>
<p><b>Note:</b> See <a href="CodeConventions.doc10.html#a182">"Java Source File Example" on page 19</a> for examples of the comment formats described here.<em></em>
<p><a name="15533"> </a>
For further details, see "How to Write Doc Comments for Javadoc" which includes information on the doc comment tags (@return, @param, @see):
</p><blockquote><a href="http://java.sun.com/products/jdk/javadoc/writingdoccomments.html">http://java.sun.com/products/jdk/javadoc/writingdoccomments.html</a> 
</blockquote><p><a name="15527"> </a>
For further details about doc comments and javadoc, see the javadoc home page at:
</p><blockquote><a href="http://java.sun.com/products/jdk/javadoc/">http://java.sun.com/products/jdk/javadoc/</a>
</blockquote><p><a name="15518"> </a>
Doc comments describe Java classes, interfaces, constructors, methods, and fields.  Each doc comment is set inside the comment delimiters <code>/**...*/</code>, with one comment per class, interface, or member.  This comment should appear just before the declaration:
</p><blockquote><pre>/**
 * The Example class provides ...
 */
public class Example { ...
</pre></blockquote><p><a name="11763"> </a>
Notice that top-level classes and interfaces are not indented, while their members are.  The first line of doc comment (/**) for classes and interfaces is not indented; subsequent doc comment lines each have 1 space of indentation (to vertically align the asterisks).  Members, including constructors, have 4 spaces for the first doc comment line and 5 spaces thereafter.
</p><p><a name="364"> </a>
If you need to give information about a class, interface, variable, or method that isn't appropriate for documentation, use an implementation block comment<a href="CodeConventions.doc4.html#a680"> (see section 5.1.1)</a> or single-line<a href="CodeConventions.doc4.html#a341"> (see section 5.1.2)</a> comment immediately <em>after</em> the declaration. For example, details about the implementation of a class should go in in such an implementation block comment <em>following</em> the class statement, not in the class doc comment.
</p><p><a name="1700"> </a>
Doc comments should not be positioned inside a method or constructor definition block, because Java associates documentation comments with the<em> </em>first declaration <em>after</em> the comment. 
</p></pre></blockquote>
<hr><br>
 
<a href="CodeConvTOC.doc.html">[Contents]</a> <a href="CodeConventions.doc3.html">[Prev]</a> <a href="CodeConventions.doc5.html">[Next]</a> 
<hr><br>

<i><a href="Copyright.doc.html">Copyright</a> &#169; 1995-1999, Sun Microsystems, Inc.   All rights
reserved.</i>


<!-- Last updated: Thu Apr 15 09:03:50 1999 -->

</body>
</html>
